Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
convert slightly complex "if" statement to ternary operator

I have the following code from a project:

setTweetLibrary((curr) => {
    if (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)) {
        console.log("update");
        return saved_tweets;
    } else {
        console.log("not update");
        return curr;
    }
});

I would like to convert this to a ternary operator with a question mark ("?") to make it neater, but I can't figure out how. Can anyone advise? Thank you!

I think I'm just really confused on how the syntax of ternary operators with multiple conditions and values

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

As ternary it would look like this:

setTweetLibrary((curr) => {
      return !curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)
        ? saved_tweets
        : curr;
});

Note that there is no more place for console.logs. But they seem to be debug anyway.

about 4 years ago · Juan Pablo Isaza Relatório

0

The ternary operator is not a generic replacement for an if statement, only for choosing between expressions that are going to the same destination.

If we take out the logging, both branches are returning a variable:

if (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id)) {
    return saved_tweets;
} else {
    return curr;
}

The fact that the condition is a long expression doesn't make any difference, it's still equivalent to this:

if (some_condition_goes_here) {
    return saved_tweets;
} else {
    return curr;
}

In that case, you could use a ternary operator to select between the two - move the return to where the if is, and the ? and : before the two values. Then put the whole thing in parentheses to make sure the whole thing is calculated before return:

return (
    (some_condition_goes_here)
    ? saved_tweets
    : curr
);

Specifically:

return (
    (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id))
    ? saved_tweets
    : curr
);

It might be possible to add the logging in there as well, but it will be much harder to read than just using a normal if statement, so if you want that - or any other logic - just stick with if...else.

about 4 years ago · Juan Pablo Isaza Relatório

0

This works

 return (!curr[0] || (curr[0] && curr[0].id !== saved_tweets[0].id) ? saved_tweets : curr)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda